//
//  XiangqingViewController.m
//  New
//
//  Created by 才诗琦 on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com. All rights reserved.
//

#import "XiangqingViewController.h"
#import "SliderView-Prefix.pch"

@interface XiangqingViewController ()<UIWebViewDelegate>
@property(nonatomic, retain)UIWebView *wv;

@end

@implementation XiangqingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //左按钮
  self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(fanHui:)] autorelease];
   self.navigationController.navigationBar.translucent = NO;
    //创建WebView
    self.wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, -50, self.view.frame.size.width, self.view.frame.size.height)];
    
    _wv.delegate = self;
    
//    NSString *str = [self.str stringByAppendingString:@"<head><style>img{max-width:320px !important;}</style></head>"];
    //解析数据
[self.wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.str]]];
    
    //失去弹动效果
    
    self.wv.scrollView.bounces = NO;
    
    
   
  
    //添加父视图
    [self.view addSubview:self.wv];
    
    
    [_wv stringByEvaluatingJavaScriptFromString:[NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil]];
    
    
    //管理内存
    [self.wv release];
    
    
}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
  
 [webView stringByEvaluatingJavaScriptFromString:@"setImageClickFunction()"];
    
     [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
    //webview加载页面后，改变字体的大小，当字体变大的时候页面会随着变大，但是当我缩小字体的时候页面还是原来的大小。
    NSString * srt = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust = '85%'";
    [webView stringByEvaluatingJavaScriptFromString:srt];
    [webView stringByEvaluatingJavaScriptFromString:
     //document.createElement()是在对象中创建一个对象 要与appendChild() 或 insertBefore()方法联合使用。其中，appendChild() 方法在节点的子节点列表末添加新的子节点。insertBefore() 方法在节点的子节点列表任意位置插入新的节点。
     //JavaScript文件的动态加载
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     //缩放图片到合适大小
     "var myimg,oldwidth;"
     "var maxwidth=320;" //缩放系数 图片宽度
     //遍历所有image
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     //WebView加载HTML图片大小自适应与文章自动换行”
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height * (maxwidth/oldwidth);"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [webView stringByEvaluatingJavaScriptFromString:srt];
    
      //字体颜色
    
    
     [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
    //页面背景色
    

     [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
   
 
    
    }

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    
    NSString *path=[[request URL] absoluteString];
    
    NSLog(@"%@",path);
    return YES;
}
//按钮实现方法
- (void)fanHui:(UIButton *)fanhui
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
